import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class IntentsHomePage extends StatefulWidget {
  const IntentsHomePage({super.key});

  @override
  State<IntentsHomePage> createState() => _IntentsHomePageState();
}

class _IntentsHomePageState extends State<IntentsHomePage> with SingleTickerProviderStateMixin {
  //当前选中的按钮索引，-1表示未选中
  int _selectedIndex = -1;
  //按钮文字
  final List<String> _buttonTexts = [
    "获取 insightlntents",
    "获取 deletelntent",
    "获取 deleteEntity",
    "getSid",
    "sharelntent",
    "deleteEntity",
    "deleteEntity",
    "getSidRenew"
  ];
// 文本控制器
  final TextEditingController _textController = TextEditingController();
  @override
  void initState() {
    super.initState();
    // 监听文本变化以更新字数统计
    _textController.addListener(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    _textController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.grey[100],
      ),
      backgroundColor: Colors.grey[100], // 设置整体背景为浅灰色
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 16),
            child: const Text(
              "Lnsightintent Test",
              style: TextStyle(
                  fontWeight: FontWeight.w700,
                  fontSize: 30,
                  color: Colors.black),
            ),
          ),

          // 输入框
          Expanded(
            child: Container(
              margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: _buildTextField(),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 16),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: _buttonTexts.asMap().entries.map((entry) {
                return _buildCustomButton(entry.key);
              }).toList(),
            ),
          )
        ],
      ),
    );
  }

  //创建按钮
  Widget _buildCustomButton(int index) {
    final isSelected = _selectedIndex == index;
    return GestureDetector(
      onTap: () => _handleButtonTap(index),
      behavior: HitTestBehavior.translucent,
      child: Container(
        margin: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
        height: 40,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: isSelected ? Colors.blue : Colors.grey[200],
            borderRadius: BorderRadius.circular(20)),
        child: Text(
          _buttonTexts[index],
          style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w600,
              color: isSelected ? Colors.white : Colors.blue),
        ),
      ),
    );
  }

  // 输入框组件
  Widget _buildTextField() {
    return TextField(
      controller: _textController,
      maxLines: 0, // 允许换行
      maxLength: 100, // 最大字符数
      maxLengthEnforcement: MaxLengthEnforcement.enforced, // 达到最大值后禁止输入
      decoration: const InputDecoration(
        border: InputBorder.none,
        hintText: "请输入或者选择内容以后再点击功能按钮，最多100个字",
        hintStyle: TextStyle(color: Colors.grey),
      ),
    );
  }

  //按钮点击响应事件
  void _handleButtonTap(int index) {
    setState(() {
      if (_selectedIndex == index) {
        _selectedIndex = -1;
      } else {
        _selectedIndex = index;
      }
    });
  }
}
